const app = getApp(), core = require('../../utils/core'),chatIM = require('../../utils/websocket.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userinfo: null,
    // userinfo:{group_id:1},
    authTipShowed: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userinfo = app.getUserinfo();
    this.setData({userinfo: userinfo});
    app.eventBus.on(app.Events.UserinfoUpdate, this.eventOnUserinfoUpdate, this);
    app.eventBus.on(app.Events.UserLogout, this.eventOnUserLogout, this);
    this.setData({
      isIPhoneX: app.globalData.isIPhoneX
    });
    //未读消息
	if (app.globalData.chat_config.SocketTask) {
		chatIM.setMessageBadge(false)
	 }
	app.eventBus.on(app.Events.WebsocketMessageCount, this.eventWebsocketMessageCount,this);

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    app.eventBus.off(app.Events.UserinfoUpdate, this.eventOnUserinfoUpdate);
	app.eventBus.off(app.Events.UserLogout, this.eventOnUserLogout);
	app.eventBus.off(app.Events.WebsocketMessageCount, this.eventWebsocketMessageCount);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar() ) {
      this.getTabBar().setData({
        selected: 4
      })
    }
  },

  eventOnUserinfoUpdate: function(userinfo) {
    this.setData({userinfo: userinfo});
  },
  eventOnUserLogout: function () {
    this.setData({userinfo: null})
  },
  onPullDownRefresh: function () {
    let component = this.selectComponent('.ucenter-frame');
    component.refresh();
    wx.stopPullDownRefresh();
  },

  eventWebsocketMessageCount(data){
	var list = this.getTabBar().data.list;
	list[1].megNum = app.globalData.chat_config.messageCount;
	this.getTabBar().setData({
	  list
	})
  },

});
